
package com.baijia.jigou.authentication.handler;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.baijia.authentication.AccountType;
import com.baijia.authentication.BaijiaAccount;
import com.baijia.authentication.handler.exception.AuthenticationException;
import com.baijia.authentication.handler.support.AbstractPreAndPostProcessingAuthenticationHandler;
import com.baijia.authentication.principal.Credentials;
import com.baijia.jigou.principal.CookieJigouCredentials;
import com.baijia.jigou.util.WebContext;


/**
 * @title JigouAuthenticationHandler
 * @desc TODO 
 * @author caoliang
 * @date 2015年11月10日
 * @version 1.0
 */
@Component
public class CookieJigouAuthenticationHandler extends AbstractPreAndPostProcessingAuthenticationHandler implements BaijiaAccount {
    
    @Override
    protected boolean doAuthentication(Credentials credentials)
        throws AuthenticationException {
        CookieJigouCredentials jigouCredentials = (CookieJigouCredentials) credentials;
        
        if (StringUtils.isNotBlank(jigouCredentials.getSessionId())) {
            try {
                return WebContext.getSession()!=null;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        
        return false;

    }
    
    @Override
    public int getAccountType(){
        return AccountType.JIGOU.getCode();
    }

    @Override
    public boolean supports(Credentials credentials) {
        return credentials instanceof CookieJigouCredentials;
    }
}

    